package cn.skillith.showcompanybackend.controller;

import cn.skillith.showcompanybackend.entity.ContactMessage;
import cn.skillith.showcompanybackend.service.ContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {
    
    @Autowired
    private ContactService contactService;
    
    /**
     * 管理员首页 - 显示所有消息
     */
    @GetMapping({"", "/", "/messages"})
    public String adminHome(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(defaultValue = "all") String status,
            Model model) {
        
        try {
            System.out.println("AdminController: 开始处理请求");
            
            Page<ContactMessage> messagePage = contactService.getMessages(page, size, status);
            long totalCount = contactService.getMessageCount(status);
            
            System.out.println("AdminController: 获取到 " + messagePage.getContent().size() + " 条消息");
            
            model.addAttribute("messages", messagePage.getContent());
            model.addAttribute("currentPage", page);
            model.addAttribute("totalPages", messagePage.getTotalPages());
            model.addAttribute("totalCount", totalCount);
            model.addAttribute("currentStatus", status);
            model.addAttribute("hasNext", messagePage.hasNext());
            model.addAttribute("hasPrevious", messagePage.hasPrevious());
            
            System.out.println("AdminController: 准备返回页面");
            return "admin/messages";
            
        } catch (Exception e) {
            System.err.println("AdminController: 发生错误 - " + e.getMessage());
            e.printStackTrace();
            
            // 返回一个简单的错误信息页面
            model.addAttribute("error", "数据库连接错误: " + e.getMessage());
            model.addAttribute("messages", java.util.Collections.emptyList());
            model.addAttribute("currentPage", 0);
            model.addAttribute("totalPages", 0);
            model.addAttribute("totalCount", 0);
            model.addAttribute("currentStatus", "all");
            model.addAttribute("hasNext", false);
            model.addAttribute("hasPrevious", false);
            
            return "admin/messages";
        }
    }
    
    /**
     * 查看单个消息详情
     */
    @GetMapping("/message/{id}")
    public String viewMessage(@PathVariable Long id, Model model) {
        ContactMessage message = contactService.getMessageById(id);
        if (message == null) {
            return "admin/error";
        }
        
        model.addAttribute("message", message);
        return "admin/message-detail";
    }
    
    /**
     * 更新消息状态
     */
    @PostMapping("/message/{id}/status")
    @ResponseBody
    public Map<String, Object> updateMessageStatus(
            @PathVariable Long id,
            @RequestParam String status,
            @RequestParam(required = false) String adminNotes) {
        
        try {
            contactService.updateMessageStatus(id, status, adminNotes);
            return Map.of("success", true, "message", "状态更新成功");
        } catch (Exception e) {
            return Map.of("success", false, "message", "更新失败: " + e.getMessage());
        }
    }
    
    /**
     * 封禁设备
     */
    @PostMapping("/device/block")
    @ResponseBody
    public Map<String, Object> blockDevice(
            @RequestParam String deviceFingerprint,
            @RequestParam(defaultValue = "24") int hours) {
        
        try {
            contactService.blockDevice(deviceFingerprint, hours);
            return Map.of("success", true, "message", "设备已封禁 " + hours + " 小时");
        } catch (Exception e) {
            return Map.of("success", false, "message", "封禁失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取设备的所有消息
     */
    @GetMapping("/device/{deviceFingerprint}/messages")
    public String getDeviceMessages(@PathVariable String deviceFingerprint, Model model) {
        var messages = contactService.getMessagesByDevice(deviceFingerprint);
        model.addAttribute("messages", messages);
        model.addAttribute("deviceFingerprint", deviceFingerprint);
        return "admin/device-messages";
    }
}
